#!/usr/bin/env python
# coding:utf-8

import pymysql, sys


# sql=sys.argv[1]

def select_mysql(sql):
    conn = pymysql.connect(host='121.201.68.21', port=3307, user='jiang', passwd='jiangwenhui', db='trans_data',
                           charset='UTF8')

    cur = conn.cursor()
    reCount = cur.execute(sql)

    nRet = cur.fetchall()
    cur.close()

    conn.close()
    return nRet


def insert_mysql(sql):
    conn = pymysql.connect(host='121.201.68.21', port=3307, user='jiang', passwd='jiangwenhui', db='daxiangzhanshi',

                           charset='UTF8')

    cur = conn.cursor()

    cur.execute(sql)
    nRet = cur.fetchall()
    conn.commit()

    cur.close()

    conn.close()
    return nRet

if __name__ == "__main__":
    insert_mysql("delete from app01_subclass")
    select_ls_category = "select sum(s.total) as zong,s.cate_id,l.name,l.parent_id,c2.name from sub_category_det s inner join ls_category  l on s.cate_id=l.id INNER JOIN ls_category c2 on c2.id=l.parent_id  where time_ >= curdate() group by cate_id order by zong desc limit 20"
    data = select_mysql(select_ls_category)
    for i in data:
#	print i
        insert_ls_category = '''insert into app01_subclass (total_rmb,name,categoriesId,categoriesName) values ('%s','%s','%s','%s') '''%(i[0],i[2],i[3],i[4])
        insert_mysql(insert_ls_category)
        #if not insert_mysql("select * from app01_real where updatetime='%s'"%i[3]):
            #insert_mysql(insert_ls_category)
	#print(insert_ls_category)
